package com.hafu.view.RuKu;

import javax.swing.*;
import javax.swing.LayoutStyle;
import javax.swing.table.*;
import com.hafu.model.GysInfo;
import com.hafu.model.RuKuDetail;
import com.hafu.model.RuKuMain;
import com.hafu.model.SpInfo;
import com.hafu.service.GysInfoService;
import com.hafu.service.RuKuService;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


/**
 * @author Administrator
 */
public class AddRuKuFrm extends JFrame {
    private GysInfoService GysInfoService = new GysInfoService();
    private SpInfoService spInfoService = new SpInfoService();
    private RuKuService ruKuService = new RuKuService();
    private TableColumn tableColumn;
    public AddRuKuFrm() {
        initComponents();
        fillComboBox();
    }
    private void fillComboBox() {
        ArrayList<GysInfo> gysInfoByName = this.GysInfoService.getGysInfoByName(new GysInfo());
        Iterator var2 = gysInfoByName.iterator();

        while(var2.hasNext()) {
            GysInfo gysInfo = (GysInfo)var2.next();
            this.comboBox1.addItem(gysInfo.getName());
        }

        JComboBox<SpInfo> comboBox = new JComboBox();
        ArrayList<SpInfo> spInfoByName = this.spInfoService.getSpInfoByName(new SpInfo());
        Iterator var4 = spInfoByName.iterator();

        while(var4.hasNext()) {
            SpInfo spInfo = (SpInfo)var4.next();
            comboBox.addItem(spInfo);
        }

        DefaultTableModel defaultTableModel = (DefaultTableModel)this.table1.getModel();
        this.tableColumn = this.table1.getColumnModel().getColumn(0);
        this.tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
    }
    private void button1(ActionEvent e) {
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.addRow(new Vector());
    }
    private void button2(ActionEvent e) {
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRukuName(textField1.getText());
        ruKuMain.setGysName(comboBox1.getSelectedItem().toString());
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        int rowCount = defaultTableModel.getRowCount();
        ArrayList<RuKuDetail> rukudetailList = new ArrayList<>();
        BigDecimal totalPrice = new BigDecimal(0);
        for (int i = 0; i < rowCount; i++) {
            SpInfo SpInfo1 = (SpInfo) table1.getValueAt(i, 0);
            if (SpInfo1 == null || StringUtil.isEmpty(SpInfo1.getName())) {
                continue;
            }

            // 获取数量列并做非空判断
            Object value = table1.getValueAt(i, 1);
            Integer num = null;
            if (value != null) {
                try {
                    num = Integer.parseInt(value.toString());
                } catch (NumberFormatException ex) {  // 修改此处变量名
                    // 如果解析失败，则跳过该行
                    continue;
                }
            } else {
                // 如果值为 null，可以选择跳过或者设置默认值
                continue; // 或者根据业务需求设为 0：num = 0;
            }

            RuKuDetail ruKuDetail = new RuKuDetail();
            ruKuDetail.setSpName(SpInfo1.getName());
            ruKuDetail.setNum(num);
            ruKuDetail.setPrice(SpInfo1.getPrice());
            rukudetailList.add(ruKuDetail);
            totalPrice = totalPrice.add(SpInfo1.getPrice().multiply(new BigDecimal(num)));
        }


        ruKuMain.setRukudetailList(rukudetailList);
        ruKuMain.setPrice(totalPrice);
        boolean b = ruKuService.addRuKu(ruKuMain);
        if (b) {
            JOptionPane.showMessageDialog(null, "添加成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - aaaa
        textField1 = new JTextField();
        textField2 = new JTextField();
        comboBox1 = new JComboBox();
        button1 = new JButton();
        button2 = new JButton();
        scrollPane2 = new JScrollPane();
        table1 = new JTable();
        label1 = new JLabel();

        //======== this ========
        setTitle("\u6dfb\u52a0\u8ba2\u5355\u4fe1\u606f");
        var contentPane = getContentPane();

        //---- textField2 ----
        textField2.setText("\u8001\u5e08\u9009\u62e9");

        //---- button1 ----
        button1.setText("\u6dfb\u52a0\u65b0\u884c");
        button1.addActionListener(e -> button1(e));

        //---- button2 ----
        button2.setText("\u63d0\u4ea4\u5165\u5e93");
        button2.addActionListener(e -> button2(e));

        //======== scrollPane2 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null},
                },
                new String[] {
                    "\u8bfe\u7a0b", "\u8bfe\u65f6\u6570\u91cf"
                }
            ));
            {
                TableColumnModel cm = table1.getColumnModel();
                cm.getColumn(1).setPreferredWidth(65);
            }
            scrollPane2.setViewportView(table1);
        }

        //---- label1 ----
        label1.setText("\u5b66\u751f\u540d\u79f0");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(96, 96, 96)
                    .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 789, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(288, Short.MAX_VALUE))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)
                    .addGap(28, 28, 28)
                    .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
                    .addGap(33, 33, 33)
                    .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 296, Short.MAX_VALUE)
                    .addComponent(button1)
                    .addGap(62, 62, 62)
                    .addComponent(button2)
                    .addGap(188, 188, 188))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(5, 5, 5)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button1))
                    .addGap(46, 46, 46)
                    .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 441, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(232, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - aaaa
    private JTextField textField1;
    private JTextField textField2;
    private JComboBox comboBox1;
    private JButton button1;
    private JButton button2;
    private JScrollPane scrollPane2;
    private JTable table1;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
